/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.presentation.actions;

import java.util.HashSet;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

import dbexel.model.dao.AttributeDao;
import dbexel.model.dao.AttributeDaoImpl;
import dbexel.model.mapping.AttributeValue;
import dbexel.model.mapping.WorkSheet_Attribute;
import dbexel.model.mapping.WorkSheet_Entry;

public class AttributeAction {

	@SuppressWarnings("unused")
	private static final long serialVersionUID = 1L;

	private Long attribute_Id;
	private String attribute_Name = "";
	private Long workSheet_Id;

	private AttributeDao attributeDao;
	private WorkSheet_Attribute attribute;

	private Set<WorkSheet_Entry> entriesPresent = new HashSet<WorkSheet_Entry>();

	public String execute() {
		setAttribute(getAttributeDao().fetchAttribute(getAttribute_Id()));

		setEntriesPresent(getAttribute().fetchEntriesUsingThis());
		if (getAttribute_Name().isEmpty()) {

			setAttribute_Name(getAttribute().getAttrName());
			return ActionSupport.INPUT;
		}

		//Update attribute name
		if (!getAttribute_Name().equals(getAttribute().getAttrName())) {
			getAttribute().setAttrName(getAttribute_Name());
			getAttributeDao().updateAttribute(getAttribute());
		}
 
		return ActionSupport.SUCCESS;
	}

	/**
	 * @return the attribute_Name
	 */
	public String getAttribute_Name() {
		return attribute_Name;
	}

	/**
	 * @param attribute_Name
	 *            the attribute_Name to set
	 */
	public void setAttribute_Name(String attribute_Name) {
		this.attribute_Name = attribute_Name;
	}

	/**
	 * @return the workSheet_Id
	 */
	public Long getWorkSheet_Id() {
		return workSheet_Id;
	}

	/**
	 * @param workSheet_Id
	 *            the workSheet_Id to set
	 */
	public void setWorkSheet_Id(Long workSheet_Id) {
		this.workSheet_Id = workSheet_Id;
	}

	/**
	 * @return the attribute_Id
	 */
	public Long getAttribute_Id() {
		return attribute_Id;
	}

	/**
	 * @param attribute_Id
	 *            the attribute_Id to set
	 */
	public void setAttribute_Id(Long attribute_Id) {
		this.attribute_Id = attribute_Id;
	}

	/**
	 * @return the attributeDao
	 */
	public AttributeDao getAttributeDao() {
		if (attributeDao == null)
			setAttributeDao(new AttributeDaoImpl());

		return attributeDao;
	}

	/**
	 * @param attributeDao
	 *            the attributeDao to set
	 */
	public void setAttributeDao(AttributeDao attributeDao) {
		this.attributeDao = attributeDao;
	}

	/**
	 * @return the attribute
	 */
	public WorkSheet_Attribute getAttribute() {
		return attribute;
	}

	/**
	 * @param attribute
	 *            the attribute to set
	 */
	public void setAttribute(WorkSheet_Attribute attribute) {
		this.attribute = attribute;
	}

	/**
	 * @return the entriesPresent
	 */
	public Set<WorkSheet_Entry> getEntriesPresent() {
		return entriesPresent;
	}

	/**
	 * @param entriesPresent
	 *            the entriesPresent to set
	 */
	public void setEntriesPresent(Set<WorkSheet_Entry> entriesPresent) {
		this.entriesPresent = entriesPresent;
	}
}
